﻿using System.Drawing;
using System.Diagnostics;

#if IOS || ANDROID || MACCATALYST
using Microsoft.Maui.Graphics.Platform;
#elif WINDOWS
using Microsoft.UI;
using Microsoft.UI.Windowing;
using Microsoft.Maui.Graphics.Win2D;
using Window = Microsoft.UI.Xaml.Window;
using AppWindow = Microsoft.UI.Windowing.AppWindow;
using AppWindowTitleBar = Microsoft.UI.Windowing.AppWindowTitleBar;
#endif


using IImage = Microsoft.Maui.Graphics.IImage;


using ymk_camera_surveillance.CaptureCamera;
using OpenCvSharp;
using CommunityToolkit.Mvvm.Messaging;
using ymk_camera_surveillance.Models;
using Colors = Microsoft.Maui.Graphics.Colors;

namespace ymk_camera_surveillance;

public class ChangeTabBarMessage : CommunityToolkit.Mvvm.Messaging.Messages.ValueChangedMessage<int>
{
    public ChangeTabBarMessage(int status) : base(status)
    {
    }
}

public class GraphicsDrawable : GraphicsView, IDrawable
{
    static public int currentEvent = 0;
    static public int maxDrawEvent = 5;

    public IImage showImg = null;

    public GraphicsDrawable()
    {

    }

    public void Draw(ICanvas canvas, RectF dirtyRect)
    {
        if (null != showImg)
        {
            canvas.DrawImage(showImg, 0, 0, showImg.Width, showImg.Height);
        }
        else
        {
            canvas.FontColor = Colors.Red;
            canvas.FontSize = 24;
            canvas.DrawString("等待视频输出", 100, 40 + 24, HorizontalAlignment.Center);
        }
    }
}

public partial class CameraVideoPage : ContentPage
{
    public ObsCamera camera { get; set; }
    private string RtspUrl = "";

    private int promoteCount = 0;
    private CapRtsp capRtsp = new CapRtsp();

    public CameraVideoPage(ObsCamera camera)
    {
        InitializeComponent();


        // Do not change the order
        this.camera = camera;
        this.RtspUrl = camera.RtspUrl;

        InitMessageSubscribe();
    }


    private void InitMessageSubscribe()
    {
        Debug.WriteLine($"创建接收方， token: {RtspUrl}");

    }

#if WINDOWS        
    private AppWindow GetAppWindowForCurrentWindow(Window xamlWindow)
    {
        xamlWindow.ExtendsContentIntoTitleBar = false;

        var currentHwnd = ((MauiWinUIWindow)App.Current.Windows[0].Handler.PlatformView).WindowHandle;
        WindowId currentWid = Win32Interop.GetWindowIdFromWindow(currentHwnd);
        return AppWindow.GetFromWindowId(currentWid);
    }
#endif

    public void OnCameraViewDoubleTapped(object sender, TappedEventArgs args)
    {

#if WINDOWS
        Debug.WriteLine("双击屏幕");
        Debug.WriteLine("AppWindowTitleBar.IsCustomizationSupported(): " + AppWindowTitleBar.IsCustomizationSupported());
        var mauiWindow = GetParentWindow();
        var xamlWindow = ((MauiWinUIWindow)mauiWindow.Handler.PlatformView) as Microsoft.UI.Xaml.Window;
        IntPtr hWnd = WinRT.Interop.WindowNative.GetWindowHandle(xamlWindow);
        WindowId myWndId = Win32Interop.GetWindowIdFromWindow(hWnd);
        var currentWindow = AppWindow.GetFromWindowId(myWndId); ;
        if (currentWindow.Presenter.Kind != AppWindowPresenterKind.FullScreen)
        {
            xamlWindow.ExtendsContentIntoTitleBar = false;
            currentWindow.SetPresenter(AppWindowPresenterKind.FullScreen);

            //隐藏导航页
            Shell.SetNavBarIsVisible(mauiWindow, false);
            Shell.SetTabBarIsVisible(mauiWindow, false);
            mauiWindow.Title = "";
        }
        else
        {
            var displayInfo = DeviceDisplay.Current.MainDisplayInfo;
            mauiWindow.Width = (displayInfo.Width / displayInfo.Density) / 2;
            mauiWindow.Height = (displayInfo.Height / displayInfo.Density) / 2;

            mauiWindow.X = (displayInfo.Width / displayInfo.Density - Window.Width) / 2;
            mauiWindow.Y = (displayInfo.Height / displayInfo.Density - Window.Height) / 2;

            xamlWindow.ExtendsContentIntoTitleBar = false;
            currentWindow.SetPresenter(AppWindowPresenterKind.Default);

            //显示导航页
            Shell.SetNavBarIsVisible(mauiWindow, true);
            Shell.SetTabBarIsVisible(mauiWindow, true);
            mauiWindow.Title = this.camera.Name + ": " + this.camera.RtspUrl;
        }
#endif
    }

    public async Task<string> PromptFillRtspInfoAsync()
    {
        string info = "请填写rtsp地址: ";
        string defaultRtsp = "rtsp://admin:lmk3312796@192.168.1.64:554/live0";
        if (Interlocked.Exchange(ref promoteCount, 1) == 0)
        {
            while (this.RtspUrl == null || this.RtspUrl == "")
            {
                Debug.WriteLine("提示 填写 rtsp 信息");
                this.RtspUrl = await DisplayPromptAsync("", info, placeholder: defaultRtsp, initialValue: defaultRtsp);
                info = "格式错误,请重新填写";
            }
        }
        else
        {
            Debug.WriteLine("跳过 填写 rtsp 信息");
        }
        Interlocked.Exchange(ref promoteCount, 0);
        return this.RtspUrl;
    }
}
